package timberman.screens;

import timberman.core.AssetUtils;
import timberman.core.MyGame;
import timberman.core.ScreenUtils;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture.TextureFilter;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.ui.Table;
import com.badlogic.gdx.utils.viewport.FitViewport;

public abstract class AbstractScreen implements Screen {
	Stage stage;
	MyGame game;
	String className;
	Skin skin;

	
	public AbstractScreen() {
		game = MyGame.getInstance();
		stage = new Stage(new FitViewport(game.assumeWidth, game.assumeHeight)) {
			@Override
			public boolean keyDown(int keyCode) {
				switch (keyCode) {
				case Keys.BACK:
					doWhenBack();
					break;

				case Keys.HOME:
					doWhenHome();
					break;

				default:
					break;
				}
				return super.keyDown(keyCode);
			}

			@Override
			public void act(float delta) {
				super.act(delta);
				acting();
			}
			@Override
			public boolean touchDown(int screenX, int screenY, int pointer,
					int button) {
				doWhenTouch(screenX,screenY	);
				return super.touchDown(screenX, screenY, pointer, button);
			}
		};
		ScreenUtils.stage = stage;
		AssetUtils.skin = game.manager.get("data/graphics/uiskin.json");
		TextureFilter minFilter = TextureFilter.Linear;
		TextureFilter magFilter = TextureFilter.Linear;
		BitmapFont font1 = AssetUtils.skin.getFont("animated");
		for (int i = 0; i < font1.getRegions().length; i++) {
			font1.getRegion(i).getTexture().setFilter(minFilter, magFilter);
		}
		className = this.getClass().getSimpleName().toString();
	}

	@Override
	public void render(float delta) {
		Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
		Gdx.gl.glClearColor(192 / 255f, 192 / 255f, 167 / 255f, 0.5f);
		stage.act(delta);
		stage.draw();
		Table.drawDebug(stage);
	}

	@Override
	public void resize(int width, int height) {
		stage.getViewport().update(width, height, true);
	}

	@Override
	public void show() {
		Gdx.input.setInputProcessor(stage);
		Gdx.input.setCatchBackKey(true);
	}

	@Override
	public void hide() {

	}

	@Override
	public void pause() {

	}

	@Override
	public void resume() {

	}

	@Override
	public void dispose() {
		stage.dispose();
	}

	public abstract void doWhenBack();

	public abstract void doWhenHome();
	
	public abstract void doWhenTouch(int x, int y);
	public abstract void acting();

	public float getDimen(String name) {
		return game.getDimen(className, name);
	}

	public void setPosition(Actor actor) {
		String name = actor.getName();
		if (name != null) {
			actor.setPosition(getDimen(name + "_x"), getDimen(name + "_y"));
		}
	}

	public void setSize(Actor actor) {
		String name = actor.getName();
		if (name != null) {
			actor.setPosition(getDimen(name + "_width"), getDimen(name
					+ "_height"));
		}
	}
	

}
